package com.example.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 日期：2025/5/14 16:34
 * 文件工具类
 */

@RestController
@RequestMapping("/files")
public class FileController {

    private static final String filePath = System.getProperty("user.dir") + "/files/";

    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        synchronized (FileController.class){
            // 生成唯一标识符
            String flag = System.currentTimeMillis() + "";
            // 获取文件名
            String fileName = file.getOriginalFilename();
            try{
                // 如果没有file文件夹，就创建文件目录
                if (!FileUtil.isDirectory(filePath)){
                    FileUtil.mkdir(filePath);
                }
                // 保存文件
                FileUtil.writeBytes(file.getBytes(),filePath + flag + "-" + fileName);
                System.out.println(fileName + "文件上传成功！");
                Thread.sleep(1000);
            } catch (Exception e){
                System.err.println(fileName + "文件上传失败！");
            }
            return Result.success(flag);
        }
    }

    // 获取文件
    @GetMapping("/{flag}")
        public void avatarPath(@PathVariable String flag, HttpServletResponse response){
            if (!FileUtil.isDirectory(filePath)){
                FileUtil.mkdir(filePath);
            }
            OutputStream os;
            List<String> fileNames = FileUtil.listFileNames(filePath);
            String avatar = fileNames.stream().filter(name -> name.contains(flag)).findAny().orElse("");
            try{
                if (StrUtil.isNotEmpty(avatar)){
                    response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(avatar, "UTF-8"));
                    response.setContentType("application/octet-stream");
                    byte[] bytes = FileUtil.readBytes(filePath + avatar);
                    os = response.getOutputStream();
                    os.write(bytes);
                    os.flush();
                    os.close();
                }
            }catch (Exception e){
                System.err.println("文件下载失败！");
            }
        }

    }
